package com.sfh.dog.ui;

import java.io.File;
import java.util.List;

import android.view.View;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.sfh.dog.DogApplication;
import com.sfh.dog.entity.Dog;
import com.sfh.dog.util.NetWorkHelper;
import com.sfh.dog.util.StringUtils;
import com.sfh.dog.util.ViewUtil;

/**
 * 修改宠物信息
 * 
 * @author Swer
 * 
 */
public class EditDogActivity extends ADogActivity
{
    public static final  String DOGID = "dogID";
    @Override
    public void init()
    {
        pBLoad.setVisibility(View.VISIBLE);
        String dogID = getIntent().getStringExtra(DOGID);
        BmobQuery<Dog> query = new BmobQuery<Dog>();
        query.addWhereEqualTo("objectId", dogID);
        query.findObjects(this, new FindListener<Dog>()
        {
            
            @Override
            public void onSuccess(List<Dog> arg0)
            {
                pBLoad.setVisibility(View.GONE);
                if (arg0 != null && arg0.size() >0)
                {
                    setDog(arg0.get(0));
                }
            }
            
            @Override
            public void onError(int arg0, String arg1)
            {
                pBLoad.setVisibility(View.GONE);
            }
        });
        
    }
    
    public void setDog(Dog dogEntity)
    {
        if (!StringUtils.isNullOrEmpty(dogEntity.getSpecialty()))
        {
            String data[] = dogEntity.getSpecialty().split(",");
            for (String item : data)
            {
                SpecialtyCheck.append(item.hashCode(), item);
            }
        }
        if (!StringUtils.isNullOrEmpty(dogEntity.getLove()))
        {
            String data[] = dogEntity.getLove().split(",");
            for (String item : data)
            {
                LoveCheck.append(item.hashCode(), item);
            }
        }
        
        if (!StringUtils.isNullOrEmpty(dogEntity.getDisgusting()))
        {
            String data[] = dogEntity.getDisgusting().split(",");
            for (String item : data)
            {
                DisgustingCheck.append(item.hashCode(), item);
            }
        }
        
        etDogName.setText(dogEntity.getName());
        tvDogType1.setText(dogEntity.getTypeDog());
        tvDogType2.setText(dogEntity.getTypeName());
        tvDogStatus.setText(dogEntity.getStatus());
        tvDogBirthday.setText(dogEntity.getBirthday());
        
        if (!StringUtils.isNullOrEmpty(dogEntity.getSpecialty()))
        {
            ViewUtil.setLove(this, lySpecialty, dogEntity.getSpecialty());
        }
        if (!StringUtils.isNullOrEmpty(dogEntity.getLove()))
        {
            ViewUtil.setLove(this, lyLove, dogEntity.getLove());
        }
        if (!StringUtils.isNullOrEmpty(dogEntity.getDisgusting()))
        {
            ViewUtil.setLove(this, lyDisgusting, dogEntity.getDisgusting());
        }
        
        rbMM.setChecked(0 == dogEntity.getSex());
        rbGG.setChecked(1 == dogEntity.getSex());
        ImageLoader.getInstance().displayImage(dogEntity.getImage(), ivDogphoto, DogApplication.options);
    }
    
    @Override
    public void saveDog()
    {
        
        // 更新
        if (!NetWorkHelper.isNetworkAvailable(getApplication()))
        {
            return;
        }
        if (isNull())
        {
            return;
        }
        
        pBLoad.setVisibility(View.VISIBLE);
        
        if (!StringUtils.isNullOrEmpty(filePath))
        {
            final BmobFile bmobFile = new BmobFile(new File(filePath));
            bmobFile.upload(this, new UploadFileListener()
            {
                @Override
                public void onSuccess()
                {
                    dogEntity.setImage(bmobFile.getFileUrl());
                    dogEntity.update(EditDogActivity.this, new UpdateListener()
                    {
                        
                        @Override
                        public void onSuccess()
                        {
                            pBLoad.setVisibility(View.GONE);
                            
                        }
                        
                        @Override
                        public void onFailure(int arg0, String arg1)
                        {
                            pBLoad.setVisibility(View.GONE);
                        }
                    });
                }
                
                @Override
                public void onFailure(int arg0, String arg1)
                {
                    pBLoad.setVisibility(View.GONE);
                }
            });
        }
        else
        {
            dogEntity.update(this, new UpdateListener()
            {
                
                @Override
                public void onSuccess()
                {
                    pBLoad.setVisibility(View.GONE);
                    
                }
                
                @Override
                public void onFailure(int arg0, String arg1)
                {
                    pBLoad.setVisibility(View.GONE);
                    
                }
            });
            
        }
        
    }
    
}
